package main

import (
	"fmt"
	"gitee.com/Cookie_XiaoD/godlock"
	"gitee.com/Cookie_XiaoD/godlock/redislock"
	"github.com/gomodule/redigo/redis"
	"log"
)

func main() {
	pool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial("tcp", "127.0.0.1:6379")
			return conn, err
		},
	}
	l, err := redislock.NewLock(pool, redislock.WithExSecond(5), redislock.WithLockName("test"))
	if err != nil {
		return
	}
	//block(l)
	nonBlock(l)
}

func block(l godlock.BlockDistributedLock) {
	l.LockBlock()
	defer l.UnlockBlock()
	log.Println("业务逻辑...")
	_, _ = fmt.Scanln()
}

func nonBlock(l godlock.NonBlockingDistributedLock) {
	success := l.Lock()
	if !success {
		return
	}
	defer l.Unlock()
	log.Println("业务逻辑...")
	_, _ = fmt.Scanln()
}
